package com.atguigu.udmp.stream.extractor.log;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.udmp.stream.extractor.UserEventExtractor;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;


/*{
        "common": {
        "ar": "12",
        "ba": "xiaomi",
        "ch": "web",
        "is_new": "0",
        "md": "xiaomi 13 Pro ",
        "mid": "mid_47",
        "os": "Android 12.0",
        "sid": "1e767c35-2534-4b23-94f5-40360dc4093e",
        "uid": "153",
        "vc": "v2.1.134"
        },
        "page": {
        "during_time": 12306,
        "item": "54088",
        "item_type": "order_id",
        "last_page_id": "order",
        "page_id": "payment"
        },
        "ts": 1698726408000
        }*/
public class StartUserEventExtractor extends UserEventExtractor {
    @Override
    public boolean match(JSONObject dataJsonObj) {
        if( dataJsonObj.getJSONObject("page")!=null
            &&dataJsonObj.getJSONObject("page").getString("last_page_id")==null
          &&dataJsonObj.getJSONObject("common").getLong("uid")!=null

        ){
            return true;
        }
        return false;
    }

    @Override
    public String extractDt(JSONObject dataJsonObj) {
        Long ts = dataJsonObj.getLong("ts");
        Date date = new Date(ts);
        String dt = DateFormatUtils.format(date, "yyyy-MM-dd");
        return dt;
    }

    @Override
    public String extractEventTime(JSONObject dataJsonObj) {
        Long ts = dataJsonObj.getLong("ts");
        Date date = new Date(ts);
        String eventTime = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
        return  eventTime;
    }

    @Override
    public Long extractUserId(JSONObject dataJsonObj) {
        return dataJsonObj.getJSONObject("common").getLong("uid");
    }

    @Override
    public String extractEventInfoJson(JSONObject dataJsonObj) {
        return dataJsonObj.getJSONObject("common").toJSONString();
    }


    //用户session_id
    @Override
    public String extractEventRecordKey(JSONObject dataJsonObj) {
        String sid = dataJsonObj.getJSONObject("common").getString("sid");
        return sid;
    }
}
